﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.ObjectModel;
using NPMonitor.DataModel;
using NPMonitor.Implementation;
using NPMonitor.Implementation.Internal;

namespace NPMonitor
{
    [Serializable]
    public class ServiceConfig
    {
        #region Private Members

        private bool isRegistered = false;

        private List<JobDefinition> jobDefinitions;

        #endregion

        #region Properties

        /// <summary>
        /// Is local machine has been registered on server, check by machine name
        /// </summary>
        public bool IsRegistered
        {
            get { return isRegistered; }
            set { isRegistered = value; }
        }

        /// <summary>
        /// Must be public for serialization
        /// </summary>
        public List<JobDefinition> JobDefinitions
        {
            get
            {
                if (jobDefinitions == null)
                {
                    jobDefinitions = new List<JobDefinition>();
                }

                return jobDefinitions;
            }
        }

        #endregion

        #region Constructors

        public ServiceConfig()
        {
        }

        #endregion

        #region 公共方法

        internal void AddJobDefinition(params JobDefinition[] jobDefinitions)
        {
            JobDefinitions.AddRange(jobDefinitions);
        }

        internal void RemoveJobDefinition(int jobId)
        {
            JobDefinitions.RemoveAll(j => j.JobId == jobId);
        }

        internal bool CheckAndUpdateJobDefinition(JobDefinition jobDefinition)
        {
            bool flag = true;

            if (jobDefinition.JobId == null || jobDefinition.JobId == 0)
            {
                flag = false;
            }
            else
            {
                JobDefinition jd = JobDefinitions.FirstOrDefault(j => j.JobId == jobDefinition.JobId);

                if (jd == null || jd.FileVersion < jobDefinition.FileVersion)
                {
                    if (jd != null)
                    {
                        JobDefinitions.Remove(jd);
                    }

                    FileManager.Download(jobDefinition, (setting, path) =>
                    {
                        try
                        {
                            JobFileInfo jfi = JobFileLoader.GetJobFileInfo(path);

                            // Downloaded file is invalid
                            flag = (jfi != null);
                        }
                        catch
                        {
                            flag = false;

                            throw;
                        }
                        finally
                        {
                            if (flag == false)
                            {
                                if (File.Exists(path))
                                {
                                    File.Delete(path);
                                }
                            }
                            else
                            {
                                JobDefinitions.Add(jobDefinition);
                            }
                        }

                        return true;
                    });
                }
            }

            return flag;
        }

        #endregion
    }
}
